package com.huawei.demo;

import com.huaweicloud.sdk.core.auth.BasicCredentials;
import com.huaweicloud.sdk.core.auth.ICredential;
import com.huaweicloud.sdk.core.exception.ConnectionException;
import com.huaweicloud.sdk.core.exception.RequestTimeoutException;
import com.huaweicloud.sdk.core.exception.ServiceResponseException;
import com.huaweicloud.sdk.core.region.Region;
import com.huaweicloud.sdk.dds.v3.DdsClient;
import com.huaweicloud.sdk.dds.v3.model.ListInstancesRequest;
import com.huaweicloud.sdk.dds.v3.model.ListInstancesResponse;
import com.huaweicloud.sdk.dds.v3.model.RestartInstanceRequest;
import com.huaweicloud.sdk.dds.v3.model.RestartInstanceRequestBody;
import com.huaweicloud.sdk.dds.v3.model.RestartInstanceResponse;
import com.huaweicloud.sdk.dds.v3.model.ShowEntityConfigurationRequest;
import com.huaweicloud.sdk.dds.v3.model.ShowEntityConfigurationResponse;
import com.huaweicloud.sdk.dds.v3.model.UpdateConfigurationParameterResult;
import com.huaweicloud.sdk.dds.v3.model.UpdateEntityConfigurationRequest;
import com.huaweicloud.sdk.dds.v3.model.UpdateEntityConfigurationResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

public class ParamsConfigDemo {
    private static final Logger logger = LoggerFactory.getLogger(ParamsConfigDemo.class.getName());

    /**
     * args[0] = "<YOUR IAM_END_POINT>"
     * args[1] = "<YOUR END_POINT>"
     * args[2] = "<YOUR AK>"
     * args[3] = "<YOUR SK>"
     * args[4] = "<YOUR INSTANCE_ID>"
     * args[5] = "<YOUR REGION_ID>"
     * args[6] = "<YOUR ENTITY_ID>"
     *
     * @param args
     */
    public static void main(String[] args) {
        if (args.length != 7) {
            logger.info("Illegal Arguments");
        }

        String iamEndpoint = args[0];
        String endpoint = args[1];

        String ak = args[2];
        String sk = args[3];

        String instanceId = args[4];
        String regionId = args[5];
        String entityId = args[6];

        ICredential auth = new BasicCredentials()
                .withIamEndpoint(iamEndpoint)
                .withAk(ak)
                .withSk(sk);

        DdsClient client = DdsClient.newBuilder()
                .withCredential(auth)
                .withRegion(new Region(regionId, endpoint))
                .build();

        // 查询指定实例的参数信息
        queryInstanceParamGroup(client, instanceId, entityId);

        // 修改需要重启生效的参数
        updateInstanceParamGroup(client, instanceId, entityId);

        // 重启实例 被修改参数如果需要重启则调用该方法，否则可以不调用该方法
        restartInstance(client, instanceId);

        // 重启完毕后再次查询指定实例的参数信息
        queryInstanceParamGroup(client, instanceId, entityId);
    }

    private static void queryInstanceDetail(DdsClient client, String instanceId) {
        ListInstancesRequest request = new ListInstancesRequest();
        request.setId(instanceId);

        try {
            ListInstancesResponse response = client.listInstances(request);
            logger.info(response.toString());
        } catch (ConnectionException e) {
            logger.error("ConnectionException", e);
        } catch (RequestTimeoutException e) {
            logger.error("RequestTimeoutException ", e);
        } catch (ServiceResponseException e) {
            logger.error("httpStatusCode: {}, errorCode: {}, errorMsg: {}", e.getHttpStatusCode(), e.getErrorCode(), e.getErrorMsg());
        }
    }

    private static void restartInstance(DdsClient client, String instanceId) {

        RestartInstanceRequest request = new RestartInstanceRequest();
        RestartInstanceRequestBody body = new RestartInstanceRequestBody();
        body.setTargetId(instanceId);

        request.setInstanceId(instanceId);
        request.withBody(body);

        try {
            RestartInstanceResponse response = client.restartInstance(request);
            logger.info(response.toString());
        } catch (ConnectionException e) {
            logger.error("ConnectionException", e);
        } catch (RequestTimeoutException e) {
            logger.error("RequestTimeoutException ", e);
        } catch (ServiceResponseException e) {
            logger.error("httpStatusCode: {}, errorCode: {}, errorMsg: {}", e.getHttpStatusCode(), e.getErrorCode(), e.getErrorMsg());
        }
    }

    private static void updateInstanceParamGroup(DdsClient client, String instanceId, String entityId) {
        UpdateEntityConfigurationRequest request = new UpdateEntityConfigurationRequest();
        UpdateConfigurationParameterResult body = new UpdateConfigurationParameterResult();

        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("net.maxIncomingConnections", "400");
        paramMap.put("connPoolMaxConnsPerHost", "600");
        body.setParameterValues(paramMap);
        body.setEntityId(entityId);

        request.setInstanceId(instanceId);
        request.setBody(body);

        try {
            UpdateEntityConfigurationResponse response = client.updateEntityConfiguration(request);
            logger.info(response.toString());
        } catch (ConnectionException e) {
            logger.error("ConnectionException", e);
        } catch (RequestTimeoutException e) {
            logger.error("RequestTimeoutException ", e);
        } catch (ServiceResponseException e) {
            logger.error("httpStatusCode: {}, errorCode: {}, errorMsg: {}", e.getHttpStatusCode(), e.getErrorCode(), e.getErrorMsg());
        }
    }

    private static void queryInstanceParamGroup(DdsClient client, String instanceId, String entityId) {
        ShowEntityConfigurationRequest request = new ShowEntityConfigurationRequest();
        request.setInstanceId(instanceId);
        request.setEntityId(entityId);

        try {
            ShowEntityConfigurationResponse response = client.showEntityConfiguration(request);
            logger.info(response.toString());
        } catch (ConnectionException e) {
            logger.error("ConnectionException", e);
        } catch (RequestTimeoutException e) {
            logger.error("RequestTimeoutException ", e);
        } catch (ServiceResponseException e) {
            logger.error("httpStatusCode: {}, errorCode: {}, errorMsg: {}", e.getHttpStatusCode(), e.getErrorCode(), e.getErrorMsg());
        }
    }
}
